//
//  JKSettingViewController.m
//  JKRecover
//
//  Created by Veer on 2018/6/8.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "JKSettingViewController.h"
#import "VRLoginViewController.h"
#import "JKWebViewController.h"

#import "XMeCell.h"

@interface JKSettingViewController () <UIAlertViewDelegate>



@end

@implementation JKSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"关于智慧消防云";
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    [self setupSection0];
}

- (void)setupSection0
{
    
    WeakSelf(weakSelf)
    XSettingItemModel *item0 = [XSettingItemModel itemWithIcon:nil name:@"官网"];
//    item0.destVcClass = [JKLoginViewController class];
    weakify(self)
    item0.executeOperation = ^(NSIndexPath *indexPath) {
        
        strongify(self)
        dispatch_async(dispatch_get_main_queue(), ^{
            JKWebViewController *webVc = [[JKWebViewController alloc] init];
            webVc.urlStr = @"http://www.wtx163.com/app/index.html";
            webVc.titleStr = @"官网";
            [self.navigationController pushViewController:webVc animated:YES];
        });
    };
    
    XSettingItemModel *item1 = [XSettingItemModel itemWithIcon:nil name:@"退出登录"];
    item1.executeOperation = ^(NSIndexPath *indexPath) {
        
        strongify(self)
        dispatch_async(dispatch_get_main_queue(), ^{
//            [self logout];
        });
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"退出登录？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alertView show];
        });
    };
    
    XSettingSectionModel *section0 = [[XSettingSectionModel alloc] init];
    section0.itemArray = [@[item0, item1] mutableCopy];
    
    [self.sectionArray addObject:section0];
    [self setupHeaderAndFooter];
}

- (void)setupHeaderAndFooter
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    // app名称
    NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    
    
    UIView *headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, kScreenWidth, 180);
    
    // app icon
    UIImageView *iconImgView = [[UIImageView alloc] init];
    iconImgView.contentMode = UIViewContentModeScaleAspectFit;
    iconImgView.image = [UIImage imageNamed:@"icon_app"];
    iconImgView.layer.cornerRadius = 10;
    iconImgView.layer.masksToBounds = YES;
    [headerView addSubview:iconImgView];
    [iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(headerView).offset(-12);
        make.centerX.mas_equalTo(headerView);
        make.width.mas_equalTo(140);
        make.height.mas_equalTo(140);
    }];
    

    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.text = [NSString stringWithFormat:@"V%@", app_Version];
    titleLbl.textColor = RGB(120, 120, 120);
    titleLbl.font = VRFont(16);
    titleLbl.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:titleLbl];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(iconImgView.mas_bottom).offset(8);
        make.centerX.mas_equalTo(headerView);
    }];
    
    // footer
//    UIView *footerView = [[UIView alloc] init];
//    footerView.frame = CGRectMake(0, 0, kScreenWidth, 60);
//
//    UIButton *finishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    finishBtn.backgroundColor = [UIColor whiteColor];
//    [finishBtn setTitle:@"退出登录" forState:UIControlStateNormal];
//    finishBtn.titleLabel.font = VRFont(16);
//    [finishBtn setTitleColor:RGB(52, 52, 52) forState:UIControlStateNormal];
//    [finishBtn setTitleColor:RGB(160, 160, 160) forState:UIControlStateHighlighted];
//    [finishBtn addTarget:self action:@selector(onLogoutBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    finishBtn.frame = CGRectMake(0, 10, kScreenWidth, 50);
//    [footerView addSubview:finishBtn];
    
    self.tableView.tableHeaderView = headerView;
//    self.tableView.tableFooterView = footerView;
}


#pragma mark networking
- (void)logout
{

    [VRUser shareUser].token = nil;
    [[VRUser shareUser] clearInfo];
    
    dispatch_async(dispatch_get_main_queue(), ^{
//        [SVProgressHUD showSuccessWithStatus:@"退出成功"];
    });
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:kLogout object:nil];
        [self.navigationController popToRootViewControllerAnimated:YES];
    });
    
}


#pragma mark - action
- (void)onLogoutBtnClick:(UIButton *)sender
{
    
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 取出数据模型
    XSettingSectionModel *sectionModel = self.sectionArray[indexPath.section];
    XSettingItemModel *itemModel = sectionModel.itemArray[indexPath.row];
    
    XMeCell *cell = [XMeCell cellWith:tableView];

    cell.model = itemModel;
    cell.lineType = LineTypeCenter;
    
    return cell;
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [self logout];
    }
}

@end
